package com.mf.morefun.util;

import android.content.Intent;

import com.alibaba.fastjson.JSON;
import com.apkfuns.logutils.LogUtils;
import com.mf.morefun.MoreFunApplication;
import com.mf.morefun.R;
import com.mf.morefun.bean.LoginBean;
import com.mf.morefun.constant.Configuration;
import com.mf.morefun.requestbean.RequestLoginBean;
import com.mf.morefun.ui.HomePagerActivity;
import com.mf.morefun.ui.userinfo.LoginActivity;
import com.mf.morefun.widget.LoginErrorDialog;
import com.zhcx.zhcxlibrary.utils.AppManager;
import com.zhcx.zhcxlibrary.utils.DateUtils;
import com.zhcx.zhcxlibrary.utils.FileUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class GetUserInfo {
    public static void initLogin(String user, String password){
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.LOGIN);
        RequestLoginBean bean = new RequestLoginBean();
        bean.setPhone(user);
        bean.setPassword(FileUtils.md5(password));
        params.setBodyContent(JSON.toJSONString(bean));
        params.setCharset("UTF-8");
        params.addHeader("Content-Type", "application/json");
        params.setConnectTimeout(10000);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtils.d(result);
                if (!StringUtils.isEmpty(result)) {
                    MoreFunApplication.loginBean = JSON.parseObject(result, LoginBean.class);
                    if (StringUtils.isEmpty(MoreFunApplication.loginBean.getResult().getUserType())){
                        MoreFunApplication.loginBean.getResult().setUserType("1");
                    }
                    try {
                        Intent mIntent = new Intent();
                        mIntent.setAction("GETUSERINFO");
                        AppManager.getAppManager().currentActivity().sendBroadcast(mIntent);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
